//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using OpenLS.Chart.UI.Internals;

namespace OpenLS.Chart.UI
{
    class ScatterSeriesToGeometryConverter : IMultiValueConverter
    {

        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var s = (ScatterSeries)values[0];
            var width = (double)values[1];
            var height = (double)values[2];
            var verticalScaler = values[3] as Scaler;
            var horizontalScaler = values[4] as Scaler;
            var isSmooth = (bool?)values[5];
            var scatterStyle = StrictConverter.Convert(values[6], ScatterStyle.Line);
            bool isReallySmooth;
            switch (scatterStyle)
            {
                    case ScatterStyle.Smooth:
                    case ScatterStyle.SmoothMarker:
                    isReallySmooth = true;
                    break;
                default:
                    isReallySmooth = false;
                    break;
            }
            if (isSmooth != null)
                isReallySmooth = isSmooth.Value;
            if (verticalScaler == null || horizontalScaler == null)
                return null;
            var points = new PointCollection();
            var xValues = new List<NumericValue>((s.XNumericValues));
            var yValues = new List<NumericValue>(s.YValues.NumericValues);
            for (int i = 0; i < xValues.Count; i++)
            {
                points.Add(new Point(horizontalScaler.GetValue(xValues[i].Value, 0, width),
                    verticalScaler.GetValue(yValues[i].Value, height, 0)));
            }
            return CurveFactory.CreateCurve(points, isReallySmooth);
        }


        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}
